//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "YoYInflationTraits.h"
using namespace Cephei::QL::Termstructures::Inflation;
#include <gen/QL/Termstructures/YoYInflationTermStructure.h>
using namespace Cephei::QL::Termstructures;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::CYoYInflationTraits (boost::shared_ptr<QuantLib::YoYInflationTraits>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYInflationTraits = NULL;
#endif
	_ppYoYInflationTraits = &childNative;
    
}
Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::CYoYInflationTraits (QuantLib::YoYInflationTraits& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phYoYInflationTraits = NULL;
#endif
	_ppYoYInflationTraits = new boost::shared_ptr<QuantLib::YoYInflationTraits> (&childNative);
    
    _YoYInflationTraitsOwner = owner;
    
}

Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::CYoYInflationTraits (CYoYInflationTraits^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYInflationTraits = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYoYInflationTraits = new boost::shared_ptr<QuantLib::YoYInflationTraits> (copy->GetShared());
        
    }
}
Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::CYoYInflationTraits (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYInflationTraits = NULL;
#endif
	if (!t->IsSubclassOf(CYoYInflationTraits::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::CYoYInflationTraits (QuantLib::Handle<QuantLib::YoYInflationTraits>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phYoYInflationTraits = &childNative;
	_ppYoYInflationTraits = &static_cast<boost::shared_ptr<QuantLib::YoYInflationTraits>>(childNative.currentLink());
    
    _YoYInflationTraitsOwner = owner;
}
Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::CYoYInflationTraits (QuantLib::Handle<QuantLib::YoYInflationTraits> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phYoYInflationTraits = &childNative;
	_ppYoYInflationTraits = &static_cast<boost::shared_ptr<QuantLib::YoYInflationTraits>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::CYoYInflationTraits (QuantLib::YoYInflationTraits childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYInflationTraits = NULL;
#endif
	_ppYoYInflationTraits = new boost::shared_ptr<QuantLib::YoYInflationTraits> (new QuantLib::YoYInflationTraits (childNative));
    
}
#endif

Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::~CYoYInflationTraits ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppYoYInflationTraits != NULL)
    {
	    delete _ppYoYInflationTraits;
        _ppYoYInflationTraits = NULL;
    }
}
Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::!CYoYInflationTraits ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppYoYInflationTraits != NULL)
    {
	    delete _ppYoYInflationTraits;
    }
}
QuantLib::YoYInflationTraits& Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::GetReference ()
{
    if (_ppYoYInflationTraits == NULL) throw gcnew NativeNullException ();
	return **_ppYoYInflationTraits;
}
boost::shared_ptr<QuantLib::YoYInflationTraits>& Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::GetShared ()
{
    if (_ppYoYInflationTraits == NULL) throw gcnew NativeNullException ();
	return *_ppYoYInflationTraits;
}
QuantLib::YoYInflationTraits* Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::GetPointer ()
{
    if (_ppYoYInflationTraits == NULL) throw gcnew NativeNullException ();
	return &**_ppYoYInflationTraits;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YoYInflationTraits>& Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::GetHandle ()
{
	if (_phYoYInflationTraits == NULL)
	{
		_phYoYInflationTraits = new Handle<QuantLib::YoYInflationTraits> (*_ppYoYInflationTraits);
	}
	return *_phYoYInflationTraits;
}
#endif
bool Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::HasNative () 
{
	return (_ppYoYInflationTraits != NULL);
}

Boolean Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::DummyInitialValue::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	bool _rv = (bool)(*_ppYoYInflationTraits)->dummyInitialValue ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::Guess (Cephei::QL::Termstructures::IYoYInflationTermStructure^ prm1, DateTime prm2)
{
    CYoYInflationTermStructure^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CYoYInflationTermStructure^> (prm1);
        _Cprm1->Lock();
        QuantLib::YoYInflationTermStructure* _prm1 = static_cast<QuantLib::YoYInflationTermStructure*> (_Cprm1->GetPointer ()); 
        QuantLib::Date _prm2 = (QuantLib::Date)ValueHelper::Convert (prm2);
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppYoYInflationTraits)->guess ( _prm1,  _prm2 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
DateTime Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::InitialDate (Cephei::QL::Termstructures::IYoYInflationTermStructure^ t)
{
    CYoYInflationTermStructure^ _Ct;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ct = safe_cast<CYoYInflationTermStructure^> (t);
        _Ct->Lock();
        QuantLib::YoYInflationTermStructure* _t = static_cast<QuantLib::YoYInflationTermStructure*> (_Ct->GetPointer ()); 
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppYoYInflationTraits)->initialDate ( _t );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ct != nullptr) _Ct->Unlock();
    }
}
Double Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::InitialGuess::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppYoYInflationTraits)->initialGuess ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::InitialValue (Cephei::QL::Termstructures::IYoYInflationTermStructure^ t)
{
    CYoYInflationTermStructure^ _Ct;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ct = safe_cast<CYoYInflationTermStructure^> (t);
        _Ct->Lock();
        QuantLib::YoYInflationTermStructure* _t = static_cast<QuantLib::YoYInflationTermStructure*> (_Ct->GetPointer ()); 
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppYoYInflationTraits)->initialValue ( _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ct != nullptr) _Ct->Unlock();
    }
}
UInt64 Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::MaxIterations::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppYoYInflationTraits)->maxIterations ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::MaxValueAfter (UInt64 prm1, Cephei::IVector<Double>^ prm2)
{
    CoVector<Double>^ _Cprm2;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _prm1 = (QuantLib::Size)ValueHelper::Convert (prm1);
        _Cprm2 = safe_cast<CoVector<Double>^> (prm2);
        _Cprm2->Lock();
        INativeVector<Double>^ _NCIprm2 = _Cprm2->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCprm2 = safe_cast<CDoubleVector^>(_NCIprm2);
        std::vector<QuantLib::Rate>& _prm2 = static_cast<std::vector<QuantLib::Rate>&> (_NCprm2->GetReference ());
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppYoYInflationTraits)->maxValueAfter ( _prm1,  _prm2 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm2 != nullptr) _Cprm2->Unlock();
    }
}
Double Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::MinValueAfter (UInt64 prm1, Cephei::IVector<Double>^ prm2)
{
    CoVector<Double>^ _Cprm2;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _prm1 = (QuantLib::Size)ValueHelper::Convert (prm1);
        _Cprm2 = safe_cast<CoVector<Double>^> (prm2);
        _Cprm2->Lock();
        INativeVector<Double>^ _NCIprm2 = _Cprm2->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCprm2 = safe_cast<CDoubleVector^>(_NCIprm2);
        std::vector<QuantLib::Rate>& _prm2 = static_cast<std::vector<QuantLib::Rate>&> (_NCprm2->GetReference ());
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppYoYInflationTraits)->minValueAfter ( _prm1,  _prm2 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm2 != nullptr) _Cprm2->Unlock();
    }
}
Cephei::QL::Termstructures::Inflation::IYoYInflationTraits^ Cephei::QL::Termstructures::Inflation::CYoYInflationTraits::UpdateGuess (Cephei::IVector<Double>^ data, Double level, UInt64 i)
{
    CoVector<Double>^ _Cdata;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cdata = safe_cast<CoVector<Double>^> (data);
        _Cdata->Lock();
        INativeVector<Double>^ _NCIdata = _Cdata->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdata = safe_cast<CDoubleVector^>(_NCIdata);
        std::vector<QuantLib::Rate>& _data = static_cast<std::vector<QuantLib::Rate>&> (_NCdata->GetReference ());
        QuantLib::Rate _level = (QuantLib::Rate)ValueHelper::Convert (level);
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	(*_ppYoYInflationTraits)->updateGuess ( _data,  _level,  _i );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdata != nullptr) _Cdata->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

